﻿using _8Group.MES.MengIService;
using _8Group.MES.MengIService.DTO;
using _8Group.MES.Model.MD主数据;
using _8Group.MES.Model.WM仓储管理;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _8Group.MES.MengService
{
    [ApiExplorerSettings(GroupName = "孟祥宇")]
    public class WmRtIssueService : ApplicationService, IWmRtIssueService
    {
        private readonly IRepository<WM_Rt_Issue, int> _rtIssueDetailRepository;
        public WmRtIssueService(IRepository<WM_Rt_Issue, int> rtIssueDetailRepository)
        {
            _rtIssueDetailRepository = rtIssueDetailRepository;
        }
        public async Task<string> GetRtIssueAsync(string? itemCode, string? batchCode,string? wareHouseName,int page, int limit)
        {
            var stock = await _rtIssueDetailRepository.GetQueryableAsync();

            stock = stock.WhereIf(!string.IsNullOrEmpty(itemCode), x => x.ItemCode.Contains(itemCode));
            stock = stock.WhereIf(!string.IsNullOrEmpty(batchCode), x => x.BatchCode.Contains(batchCode));
            stock = stock.WhereIf(!string.IsNullOrEmpty(wareHouseName), x => x.WarehouseName.Contains(wareHouseName));

            int totalCount = stock.Count();

            stock = stock.OrderByDescending(c => c.Id).Skip((page - 1) * limit).Take(limit);

            return JsonConvert.SerializeObject(new { code = "0", data = stock.ToList(), count = totalCount, msg = "success" }, Formatting.Indented);
        }

        public async Task<int> AddRtIssueAsync(WmRtIssueDTO WmRtIssue)
        {
            var n = AutoMapperExt.MapTo<WM_Rt_Issue>(WmRtIssue);
            await _rtIssueDetailRepository.InsertAsync(n);
            return 1;
        }

        public async Task<int> UpdateRtIssueAsync(UpdateWmRtIssueDTO WmRtIssue)
        {
            var model = AutoMapperExt.MapTo<WM_Rt_Issue>(WmRtIssue);
            await _rtIssueDetailRepository.UpdateAsync(model);
            return 1;

        }

        public async Task<int> DeleteRtIssueAsync(int id)
        {
            await _rtIssueDetailRepository.DeleteAsync(id, true);
            return 1;
        }

    }
}
